package fangbing.util.page;


public class Pagination {

    private static final Integer DEFAULTPAGESIZE = 25;

    public static int PAGESIZE = 25;

    /**
     * 起始页
     */
    private Integer startRow;

    /**
     * 结束页
     */
    private Integer endRow;

    /**
     * 每页显示条数
     */
    private Integer pageSize;

    /**
     * 当前页
     */
    private Integer pageNo;

    /**
     * 总页数
     */
    private Integer pageCount;

    private Integer totalCount;


    public Integer getPageCount() {
        if (pageNo != null) {
            pageNo = pageNo > pageCount ? pageCount : pageNo;
            endRow = getEndRow();
        }
        return pageCount;
    }

    public void setPageCount(Integer pageCount) {

        this.pageCount = pageCount;

    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        if (totalCount != 0) {
            this.totalCount = totalCount;
            pageSize = pageSize == null ? DEFAULTPAGESIZE : pageSize;
            pageCount = (totalCount + (pageSize - 1)) / pageSize;
            if (pageNo != null) {
                pageNo = pageNo > pageCount ? pageCount : pageNo;
            }
        } else {
            this.totalCount = 0;
            pageCount = 1;
            pageNo = 1;
        }
    }

    public Integer getPageNo() {
        return pageNo == null || pageNo == 0 ? Integer.valueOf(1) : pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getStartRow() {

        if (pageNo == null || pageNo == 1) {
            startRow = 0;
        } else {
            startRow = (pageNo - 1) * pageSize;
        }
        return startRow;

    }

    public void setStartRow(Integer startRow) {
        this.startRow = startRow;
    }

    public Integer getEndRow() {

        if (pageNo == null || pageNo == 1) {
            endRow = pageSize;
        } else {
            endRow = pageNo * pageSize;
        }
        return endRow;

    }

    public void setEndRow(Integer endRow) {
        this.endRow = endRow;
    }

    public Integer getPageSize() {

        return pageSize == null ? DEFAULTPAGESIZE : pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
        startRow = getStartRow();
        endRow = getEndRow();
    }

}
